package cn.zjitc.controller;


import cn.zjitc.domain.User;
import cn.zjitc.domain.VO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {


    @RequestMapping("/quick23")
    @ResponseBody
    public void save23(String username, MultipartFile[] uploadFile) throws IOException {
        System.out.println(username);
        for (MultipartFile multipartFile : uploadFile) {
            String originalFilename = multipartFile.getOriginalFilename();
            multipartFile.transferTo(new File("C:\\upload\\"+originalFilename));
        }
    }


    @RequestMapping("/quick22")
    @ResponseBody
    public void save22(String username, MultipartFile uploadFile, MultipartFile uploadFile2) throws IOException {
        System.out.println(username);
        // 获得上传文件的名称
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("C:\\upload\\"+originalFilename));

        String originalFilename2 = uploadFile.getOriginalFilename();
        uploadFile2.transferTo(new File("C:\\upload\\"+originalFilename2));
    }


    @RequestMapping("/quick21")
    @ResponseBody
    public void save21(@CookieValue(value = "JSESSIONID", required = false) String jsessionId) throws IOException {
        System.out.println(jsessionId);
    }


    @RequestMapping("/quick20")
    @ResponseBody
    public void save20(@RequestHeader(value = "User-Agent", required = false) String user_agent) throws IOException {
        System.out.println(user_agent);
    }


    @RequestMapping("/quick19")
    @ResponseBody
    public void save19(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
    }


    @RequestMapping("/quick18")
    @ResponseBody
    public void save18(Date date) throws IOException {
        System.out.println(date);
    }


    // localhost:8080/user/quick17/zhangsan
    @RequestMapping("/quick17/{username}")
    @ResponseBody
    public void save17(@PathVariable("username") String username) throws IOException {
        System.out.println(username);
    }


    @RequestMapping("/quick16")
    @ResponseBody
    public void save16(@RequestParam(value = "name") String username) throws IOException {
        /*
        * @RequestParam 参数
        * value：请求参数名称
        * required：在指定的请求参数是否必须包括，默认是true（提交时如果没有此参数则报错）
        * defaultValue：当没有指定请求参数时，则使用指定的默认值赋值
        * */
        System.out.println(username);
    }


    @RequestMapping("/quick15")
    @ResponseBody
    public void save15(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);
    }


    @RequestMapping("/quick14")
    @ResponseBody
    public void save14(VO vo) throws IOException {
        System.out.println(vo);
    }


    @RequestMapping("/quick13") // http://localhost/spring_mvc/user/quick13?strs=aaa&strs=bbb
    @ResponseBody
    public void save13(String[] strs) throws IOException {
        System.out.println(Arrays.asList(strs));
    }


    @RequestMapping("/quick12") // http://localhost/spring_mvc/user/quick12?username=aaa&age=12
    @ResponseBody
    public void save12(User user) throws IOException {
        System.out.println(user);
    }


    @RequestMapping("/quick11") // http://localhost/spring_mvc/user/quick11?username=aaa&age=12
    @ResponseBody
    public void save11(String username, int age) throws IOException {
        System.out.println(username);
        System.out.println(age);
    }


    @RequestMapping("/quick10")
    @ResponseBody
    // 期望 SpringMVC 自动将 User 转换成 json 格式的字符串
    public User save10() throws IOException {
        User user = new User();
        user.setUsername("wzw");
        user.setAge(31);
        return user;
    }


    @RequestMapping("/quick9")
    @ResponseBody
    public String save9() throws IOException {
        User user = new User();
        user.setUsername("wzw");
        user.setAge(31);
        // 使用 json 的转换工具将对象转换成 json 格式字符串再返回
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        return json;
    }


    @RequestMapping("/quick8")
    @ResponseBody
    public String save8() throws IOException {
        return "{\"username\": \"zhangsan\",\"age\": 18}";
    }


    @RequestMapping("/quick7")
    @ResponseBody // 告知SpringMVC框架，不进行视图跳转，直接进行数据响应
    public String save7() throws IOException {
        return "hello wzw";
    }


    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().println("hello wzw");
    }


    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request){
        request.setAttribute("username","好烦");
        return "success";
    }


    @RequestMapping("/quick4")
    public String save4(Model model){
        model.addAttribute("username","王卓玮");
        return "success";
    }

    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView){
        // 设置模型数据
        modelAndView.addObject("username","wzw");
        // 设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("/quick2")
    public ModelAndView save2(){
        /*
        * Model：模型，作用封装数据
        * View：视图，作用展示数据
        * */
        ModelAndView modelAndView = new ModelAndView();
        // 设置模型数据
        modelAndView.addObject("username","zcb");
        // 设置视图名称
        modelAndView.setViewName("success");
        return modelAndView;
    }


    @RequestMapping(value = "/quick", method = RequestMethod.GET, params = {"username"}) // 请求地址 http://localhost:8080/user/quick
    // http://localhost:8080/user/quick?username=xxx
    // RequestMapping 的属性，value用于指定请求的url，method指定请求的方式，params指定限制请求参数的条件
    public String save(){
        System.out.println("Controller save running...");
//        return "/success.jsp";
//        return "forward:/success.jsp";
//        return "redirect:/success.jsp";
//        return "/jsp/success.jsp";
        return "success";
    }
}
